package com.bookcloud.java21.chapter9.读取字节流文件91;

import java.io.FileInputStream;
import java.io.IOException;

public class ReadByteFileExample {
  public static void main(String[] args) {
    // 通过字节流读取文件
    byte[] data = new byte[1024];
    int bytesRead;
    // 1. 创建文件输入流
    try (FileInputStream fis = new FileInputStream("d:\\mydata.txt")) {
      while ((bytesRead = fis.read(data)) != -1) {
        System.out.println("data:" + data);
        // 打印出data二进制文件
        for (int i = 0; i < bytesRead; i++) {
          System.out.print(Integer.toBinaryString(data[i] & 0xFF) + " ");
        }
        System.out.println();

        // 把字节数组转换为字符串
        String str = new String(data, 0, bytesRead);
        System.out.print(str);
      }
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
}
